﻿using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using generic_class;
using gp_class;
using my_database;

namespace pallete_main
{
    public enum _paper_type
    {
        TYPE_PAPER = 1,
        TYPE_EXCEL = 2,
        TYPE_BOTH = 3
    }

    public partial class export : Form
    {
        int[] m_device = null;
        _paper_type m_type = 0;
        public export(int[] device,_paper_type type)
        {
            if ((null != device) && (device.Length != 0))
            {
                m_device = new int[device.Length];
                Array.Copy(device, m_device, device.Length);
            }

            m_type = type;
            InitializeComponent();
        }

        private void export_Load(object sender, EventArgs e)
        {

            MyDatabase db = new MyDatabase();

            db.init_connection(6);
            ArrayList lst = db.get_group_by_parent(6, 1);

            for (int nCount = 0; nCount < lst.Count; nCount++)
            {
                group_item item = (group_item)lst[nCount];
                cbGroup.Items.Add(item.name);
            }


            cmStage.Items.Add("可研");
            cmStage.Items.Add("初设");
            cmStage.Items.Add("施工图");
            cmStage.Items.Add("竣工图");
            cmStage.SelectedIndex = 2;

            db.close_connection();
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if ((m_device == null) || (m_device.Length == 0))
                return;

            if(txName.TextLength == 0)
            {
                MessageBox.Show("请输入图纸名称");
                return;
            }

            if(txNumber.TextLength == 0)
            {
                MessageBox.Show("请输入图纸编号");
                return;
            }

            if(cbGroup.SelectedIndex == -1)
            {
                MessageBox.Show("请选择卷册名称");
                return;
            }

            if (!Directory.Exists("c:\\cad\\"))
            {
                Directory.CreateDirectory("c:\\cad\\");
            }

            if (File.Exists("c:\\cad\\" + txName.Text))
            {
                MessageBox.Show("文件已经存在");
                return;
            }

            switch (m_type)
            {
                case _paper_type.TYPE_PAPER:
                    {
                        this.Text = "导出图纸";
                        module_gllxt gl = new module_gllxt();
                        gl.init(6);
                        gl.DrawTable("c:\\cad\\" + txName.Text, m_device, progressBar1,35);
                        gl.close();
                    }

                    break;
                case _paper_type.TYPE_EXCEL:
                    {
                        if ((null != m_device) && (m_device.Length != 0))
                        {
                            this.Text = "导出excel表格";
                            module_excel_gllxt excel = new module_excel_gllxt();
                            excel.init(6);
                            excel.DrawTable(null, m_device, progressBar1);
                            excel.close();
                            MessageBox.Show("导出完成");
                        }
                        else
                        {
                            MessageBox.Show("没有选择出图的设备");
                        }
                    }
                    break;
                case _paper_type.TYPE_BOTH:
                    {

                        if ((null != m_device) && (m_device.Length != 0))
                        {
                            this.Text = "导出图纸";
                            module_gllxt gl = new module_gllxt();
                            gl.init(6);
                            gl.DrawTable("c:\\cad\\" + txName.Text, m_device, progressBar1,0);
                            gl.close();

                            this.Text = "导出excel表格";
                            module_excel_gllxt excel = new module_excel_gllxt();
                            excel.init(6);
                            excel.DrawTable(null, m_device, progressBar1);
                            excel.close();
                            MessageBox.Show("导出完成");
                        }
                        else
                        {
                            MessageBox.Show("没有选择出图的设备");
                        }

                    }

                    break;
            }

            this.Close();

        }

        private void cmStage_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void txNumber_TextChanged(object sender, EventArgs e)
        {

        }

        private void cbGroup_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void txName_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
